package com.flower8.controller;

import com.flower8.utils.ftpUtil;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description:
 * @Author: tanglin
 * @Date: 2019/11/27 17:03
 * @Version: 1.0
 */
@RestController
@RequestMapping("/upload")
public class UploadController extends BaseController{
    private static final Logger log = LoggerFactory.getLogger(BannerController.class);

    /**
     * 图片上传
     * @param pic
     * @return
     */
    @RequestMapping("/uploadPicFile")
    public Object uploadFile(@RequestParam MultipartFile pic,int type){
        logBefore(log, "++++++++++ 图片上传 ++++++++++");
        try {
            String filePath = "";
            filePath = ftpUtil.uploadFile(pic,type);//主要就是这里实现了ftp的文件上传
            if (!"".equals(filePath)) {
                log.info("上传文件成功!");
                logAfter(log);
                Map<String, Object> data = new HashMap<>();
                data.put("url", filePath);
                return responseResultForSuccess(data, "上传图片成功");
            }else{
                return responseResultForError("上传图片失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("上传图片失败");
        }
    }

    /**
     * 删除服务器的图片
     * @param fileName 图片名称
     * @param type 删除类型
     * @return
     */
    @RequestMapping("/delPicFile")
    public Object delPicFile(@RequestParam String fileName,int type){
        logBefore(log, "++++++++++ 删除服务器的图片 ++++++++++");
        try {
            boolean res = ftpUtil.delFile(fileName,type);//主要就是这里实现了ftp的文件上传
            if (res) {
                log.info("删除服务器的图片成功!");
                logAfter(log);
                return responseResultForSuccess(null, "删除图片成功");
            }else{
                return responseResultForError("删除图片失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("删除图片失败");
        }
    }
}
